﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Xml;
using System.IO;
using Finance.Core.Configuration;

namespace Finance.Core.Provider.XmlProvider
{
    public partial class XmlProvider : FinanceProvider
    {
        /// <summary>
        ///     Gets _Folder.
        /// </summary>
        internal string StorageFolder
        {
            get
            {
                var p = this.StorageLocation().Replace("~/", string.Empty);
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p);
            }
        }

        /// <summary>
        /// The storage location is to allow Blog Providers to use alternative storage locations that app_data root directory.
        /// </summary>
        /// <returns>
        /// The storage location.
        /// </returns>
        public override string StorageLocation()
        {
            return String.IsNullOrEmpty(ConfigManager.Instance.AppSettings["StorageLocation"]) ? @"~/app_data/" : ConfigManager.Instance.AppSettings["StorageLocation"];
        }

        /// <summary>
        /// Loads the settings from the provider.
        /// </summary>
        /// <returns>A StringDictionary.</returns>
        public override StringDictionary LoadSettings()
        {
            var filename = string.Format("{0}settings.xml", this.StorageFolder);
            if (!System.IO.File.Exists(filename))
                return new StringDictionary();
            var dic = new StringDictionary();

            var doc = new XmlDocument();
            doc.Load(filename);

            var settings = doc.SelectSingleNode("settings");
            if (settings != null)
            {
                foreach (XmlAttribute settingsNode in settings.Attributes)
                {
                    var name = settingsNode.Name;
                    var value = settingsNode.InnerText;

                    dic.Add(name, value);
                }
            }
            return dic;
        }

        /// <summary>
        /// Saves the settings to the provider.
        /// </summary>
        /// <param name="settings">
        /// The settings.
        /// </param>
        public override void SaveSettings(System.Collections.Specialized.StringDictionary settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            var filename = string.Format("{0}settings.xml", this.StorageFolder);
            var writerSettings = new XmlWriterSettings { Indent = true };

            // ------------------------------------------------------------
            // Create XML writer against file path
            // ------------------------------------------------------------
            using (var writer = XmlWriter.Create(filename, writerSettings))
            {
                writer.WriteStartElement("settings");

                foreach (string key in settings.Keys)
                {
                    writer.WriteAttributeString(key, settings[key]);
                }

                writer.WriteEndElement();
            }
        }
    }
}
